const app = getApp()

Page({
  data: {
    mode: 'add', // 'add' 或 'edit'
    activityId: '',
    taskIndex: -1,
    name: '',
    content: '',
    answerTypeIndex: 0,
    answerTypes: ['文本', '选择题'],
    options: []
  },

  onLoad: function (options) {
    const { mode, activityId, taskIndex } = options
    this.setData({
      mode,
      activityId,
      taskIndex: taskIndex ? parseInt(taskIndex) : -1
    })

    if (mode === 'edit') {
      this.loadTaskData()
    }
  },

  loadTaskData: function () {
    wx.showLoading({
      title: '加载中'
    })

    wx.cloud.callFunction({
      name: 'getActivityDetail',
      data: {
        activityId: this.data.activityId
      },
      success: res => {
        const task = res.result.data.tasks[this.data.taskIndex]
        this.setData({
          name: task.name,
          content: task.content,
          answerTypeIndex: task.answerType === 'text' ? 0 : 1,
          options: task.options || []
        })
      },
      fail: err => {
        console.error('获取任务数据失败：', err)
        wx.showToast({
          title: '获取任务数据失败',
          icon: 'none'
        })
      },
      complete: () => {
        wx.hideLoading()
      }
    })
  },

  onNameInput: function (e) {
    this.setData({
      name: e.detail.value
    })
  },

  onContentInput: function (e) {
    this.setData({
      content: e.detail.value
    })
  },

  onAnswerTypeChange: function (e) {
    const index = parseInt(e.detail.value)
    this.setData({
      answerTypeIndex: index,
      options: index === 1 ? [''] : []
    })
  },

  onOptionInput: function (e) {
    const { index } = e.currentTarget.dataset
    const { value } = e.detail
    const options = [...this.data.options]
    options[index] = value
    this.setData({ options })
  },

  addOption: function () {
    if (this.data.options.length >= 6) {
      wx.showToast({
        title: '最多添加6个选项',
        icon: 'none'
      })
      return
    }
    this.setData({
      options: [...this.data.options, '']
    })
  },

  deleteOption: function (e) {
    const { index } = e.currentTarget.dataset
    const options = [...this.data.options]
    options.splice(index, 1)
    this.setData({ options })
  },

  handleSubmit: function () {
    const { name, content, answerTypeIndex, options } = this.data

    // 表单验证
    if (!name || !content) {
      wx.showToast({
        title: '请填写完整信息',
        icon: 'none'
      })
      return
    }

    if (answerTypeIndex === 1) {
      if (options.length < 2) {
        wx.showToast({
          title: '请至少添加两个选项',
          icon: 'none'
        })
        return
      }

      if (options.some(option => !option)) {
        wx.showToast({
          title: '请填写完整选项内容',
          icon: 'none'
        })
        return
      }
    }

    const taskData = {
      name,
      content,
      answerType: answerTypeIndex === 0 ? 'text' : 'choice',
      options: answerTypeIndex === 1 ? options : []
    }

    wx.showLoading({
      title: '保存中'
    })

    // 调用云函数保存任务
    wx.cloud.callFunction({
      name: this.data.mode === 'add' ? 'addTask' : 'updateTask',
      data: {
        activityId: this.data.activityId,
        taskIndex: this.data.taskIndex,
        task: taskData
      },
      success: () => {
        wx.showToast({
          title: '保存成功',
          icon: 'success',
          duration: 1500,
          success: () => {
            setTimeout(() => {
              wx.navigateBack()
            }, 1500)
          }
        })
      },
      fail: err => {
        console.error('保存任务失败：', err)
        wx.showToast({
          title: '保存失败',
          icon: 'none'
        })
      },
      complete: () => {
        wx.hideLoading()
      }
    })
  }
}) 